<div id="category-modal" style="top: 20%;" class="modal fade modal_small" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title"></h4>
            </div>
            <form action="<?php echo getUrl('product/category/saveCustom');?>" class="form-horizontal" id="category-form" method="post">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="control-label col-md-2">一级分类<span class="required">*</span></label>
                        <div class="col-md-9">
                            <select class="form-control one" data-placeholder="请选择所属分类">
                                <?php foreach ($catOne as $v): ?>
                                    <option value="<?php echo $v['id']?>" ><?php echo $v['cat_name']?></option>
                                <?php endforeach;?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-2">二级分类<span class="required">*</span></label>
                        <div class="col-md-9">
                            <select class="form-control" name="category_id" data-placeholder="请选择所属分类">
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-2">分类名称<span class="required">*</span></label>
                        <div class="col-md-9">
                            <input class="form-control required" name="cat_name" type="text">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-2">状态<span class="required">*</span></label>
                        <div class="col-md-9" style="margin-left: 18px;">
                            <div class="radio-list">
                                <label class="radio-inline"><input name="status" value="2" type="radio" checked> 启用 </label>
                                <label class="radio-inline" style="padding-left: 20px"><input name="status"  value="1" type="radio"> 禁用 </label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <input type="hidden" name="merchant_id" value="">
                    <input type="hidden" name="id" value="">
                    <button class="btn green">保存</button>
                    <button class="btn default" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i> 关闭</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function(){
        var merchantId = '<?php echo $info['id']?>';
        var model = $('#category-modal');

        $(".category-add").live('click',function () {
            model.find('.modal-title').html('添加分类' );
            model.find('.has-error').removeClass('has-error').find('.validate_message').remove();
            model.find('.show_message_error').remove();
            model.find('[name="cat_name"]').val('');
            var vl =model.find('.one').find('option').eq(0).attr('value');
            model.find('.one').val(vl).change();
            model.find('[type="radio"]').attr('checked', false);
            model.find('[type="radio"][value="2"]').attr('checked', true);
            model.find('[type="radio"]').uniform();
            model.find('[name="merchant_id"]').val(merchantId);
            model.modal();
        });

        $('.category-edit-btn').live('click', function(){
            var url = $(this).attr('href');
            l_blockUI();
            $.ajax(url, {
                dataType:'json',
                type:'get',
                error:function(){
                    show_message({status:0, info:'请求超时'});
                    App.unblockUI();
                },
                timeout:60000,
                success:function(data,textStatus,jqXHR){
                    if(!data){
                        show_message({status:0, info:'请求超时'});
                        return;
                    }
                    model.find('.modal-title').html('编辑分类 - '+ data.cat_name );
                    model.find('.has-error').removeClass('has-error').find('.validate_message').remove();
                    model.find('.show_message_error').remove();
                    for(var k in data){
                        if(k == 'status'){
                            model.find('input[type="radio"]').eq(0).attr("checked",'checked');
                            model.find('[type="radio"]').attr('checked', false);
                            model.find('[type="radio"][value="'+data.status+'"]').attr('checked', true);
                            model.find('[type="radio"]').uniform();
                        }else if(k == 'parent_id'){
                            model.find('.one').val(data[k]).change();
                        }else{
                            model.find('[name="'+k+'"]').val(data[k]);
                        }
                    }
                    model.modal();
                    App.unblockUI();
                }
            });

            return false;
        });

        $.e_validate.init('#category-form',{modal:model,ignore:'',successCallback:function(){
            $('#datatable_category').dataTable().fnDraw();
        }});

        model.find(".one").live('change',function () {
            getCategory($(this).val());
        });

        function getCategory(id){
            $.ajax("<?php echo getUrl('merchant/getCategoryTwo')?>", {
                dataType:'json',
                data:{id:id},
                type:'post',
                error:function(){
                    show_message({status:0, info:'请求超时'});
                    App.unblockUI();
                },
                timeout:60000,
                success:function(data,textStatus,jqXHR){
                    if(!data){
                        show_message({status:0, info:'请求超时'});
                        return;
                    }
                    if (data.status == 0){
                        model.find('[name="category_id"]').html('');
                        show_modal_message({status:0, message:data.message},model.find('.modal-body'));
                        return false;
                    }
                    var html;
                    $.each(data.data,function (k, v) {
                        html += '<option value="'+v.id+'">'+v.cat_name+'</option>';
                    });
                    model.find('[name="category_id"]').html(html);
                }
            });
        }
    });
</script>